package com.atguigu.tingshu.order.api;


import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

	@Autowired
	private OrderInfoService orderInfoService;


	/**
	 * 三种商品（VIP会员、专辑、声音）订单结算
	 * @param tradeVo
	 * @return
	 */
	@Operation(summary = "三种商品（VIP会员、专辑、声音）订单结算")
	@GuiGuLogin
	@PostMapping("/orderInfo/trade")
	public Result<OrderInfoVo> trade(@RequestBody TradeVo tradeVo){
		Long userId = AuthContextHolder.getUserId();
		OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo,userId);
		return Result.ok(orderInfoVo);
	}


	/**
	 * 提交订单（付款方式：1.余额 2.微信）
	 * @param orderInfoVo
	 * @return
	 */
	@Operation(summary = "提交订单（付款方式：1.余额 2.微信）")
	@GuiGuLogin
	@PostMapping("/orderInfo/submitOrder")
	public Result<Map<String, String>> submitOrder(@RequestBody OrderInfoVo orderInfoVo){
		Long userId = AuthContextHolder.getUserId();
		Map<String, String> map = orderInfoService.submitOrder(orderInfoVo,userId);
		return Result.ok(map);
	}


	/**
	 * 根据订单编号查询订单
	 * @param orderNo
	 * @return
	 */
	@Operation(summary = "根据订单编号查询订单")
	@GetMapping("/orderInfo/getOrderInfo/{orderNo}")
	public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo){
		OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
		return Result.ok(orderInfo);
	}


	/**
	 * 分页查询用户订单列表
	 * @param page
	 * @param limit
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "分页查询用户订单列表")
	@GetMapping("/orderInfo/findUserPage/{page}/{limit}")
	public Result<Page<OrderInfo>> findUserPage(@PathVariable Long page,
												@PathVariable Long limit){
		Long userId = AuthContextHolder.getUserId();
		Page<OrderInfo> pageInfo = new Page<>(page, limit);
		pageInfo = orderInfoService.findUserPage(pageInfo, userId);
		return Result.ok(pageInfo);
	}


	/**
	 * 更新订单支付信息
	 * @param orderNo
	 * @return
	 */
	@Operation(summary = "更新订单信息")
	@GetMapping("/orderInfo/orderPaySuccess/{orderNo}")
	public Result orderPaySuccess(@PathVariable String orderNo){
		orderInfoService.orderPaySuccess(orderNo);
		return Result.ok();
	}
}

